package com.ithaipeng.redanttakeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithaipeng.redanttakeaway.exception.CustomException;
import com.ithaipeng.redanttakeaway.mapper.CategoryMapper;
import com.ithaipeng.redanttakeaway.pojo.Category;
import com.ithaipeng.redanttakeaway.pojo.Dish;
import com.ithaipeng.redanttakeaway.pojo.Setmeal;
import com.ithaipeng.redanttakeaway.service.ICategoryService;
import com.ithaipeng.redanttakeaway.service.IDishService;
import com.ithaipeng.redanttakeaway.service.ISetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements ICategoryService {
    @Autowired
    private IDishService dishService;
    @Autowired
    private ISetmealService setmealService;
    @Override
    public void delete(Long id) {
        //判断菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper=new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        if(dishService.count(dishLambdaQueryWrapper)>0){
            throw new CustomException("当下分类关联了菜品,不能删除!");
        }
        //判读套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper=new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        if (setmealService.count(setmealLambdaQueryWrapper)>0){
            throw new CustomException("当下分类关联了套餐,不能删除!");
        }
        //正常删除分类
        this.removeById(id);
    }
}
